﻿Public Class FrmEquivalencia

    Private Sub FrmEquivalencia_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        cargargGvEqui()
        tbFecha.Text = Date.Now.ToLongDateString.ToString
    End Sub

    Protected Overrides Sub bnNuevo_Click(sender As Object, e As System.EventArgs)
        MyBase.bnNuevo_Click(sender, e)
        bloqCtrs()
        btBuscProd1.Focus()
    End Sub

    Protected Overrides Sub bnGuardar_Click(sender As Object, e As System.EventArgs)
        If Not Validaciones.camposVacios(pBase) Then
            actuAgrega()
            cargargGvEqui()
            MyBase.bnGuardar_Click(sender, e)
        End If
    End Sub

    Protected Overrides Sub bnEditar_Click(sender As Object, e As System.EventArgs)
        MyBase.bnEditar_Click(sender, e)
        bloqCtrs()
    End Sub

    Protected Overrides Sub bnEliminar_Click(sender As Object, e As System.EventArgs)
        If Not Validaciones.camposVacios(pBase) Then
            Dim equi As GBDZafiro.Equivalencia = crearEqui()
            If GBDZafiro.Equivalencia.exisEqui(equi) Then
                If eliminarEqui() Then
                    MyBase.bnEliminar_Click(sender, e)
                    cargargGvEqui()
                End If
            End If
        End If
    End Sub

    Private Sub btBuscProd1_Click(sender As System.Object, e As System.EventArgs) Handles btBuscProd1.Click
        Dim prod As GBDZafiro.Producto = buscarProducto()
        If prod IsNot Nothing Then
            tbProd1.Text = prod.codProducto
            tbDescripcion1.Text = prod.descripcion
            tbUnd1.Text = prod.idUniMed
        End If
    End Sub

    Private Sub btBuscProd2_Click(sender As System.Object, e As System.EventArgs) Handles btBuscProd2.Click
        Dim prod As GBDZafiro.Producto = buscarProducto()
        If prod IsNot Nothing Then
            tbProd2.Text = prod.codProducto
            tbDescripcion2.Text = prod.descripcion
            tbUnd2.Text = prod.idUniMed
        End If
    End Sub

    Private Sub tbCantidad1_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles tbCantidad1.KeyPress
        Validaciones.SoloNumeros(sender, e)
    End Sub

    Private Sub tbCantidad2_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles tbCantidad2.KeyPress
        Validaciones.SoloNumeros(sender, e)
    End Sub

    Private Sub tbCantidad1_Leave(sender As System.Object, e As System.EventArgs) Handles tbCantidad1.Leave
        Validaciones.formatMoney(sender)
    End Sub

    Private Sub tbCantidad2_Leave(sender As System.Object, e As System.EventArgs) Handles tbCantidad2.Leave
        Validaciones.formatMoney(sender)
    End Sub

    Private Sub gvEqui_CurrentCellChanged(sender As System.Object, e As System.EventArgs)
        cargarEqui()
    End Sub

    Private Sub gvEqui_CellDoubleClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs)
        cargarEqui()
    End Sub

    Private Sub btImprimir_Click(sender As System.Object, e As System.EventArgs) Handles btImprimir.Click
        Dim frmRptEqui As New frmRptEqui()
        frmRptEqui.ShowDialog(Me)
    End Sub

    Private Function crearEqui() As GBDZafiro.Equivalencia
        Dim equi As New GBDZafiro.Equivalencia
        equi.COD_PRODUCTO = tbProd1.Text
        equi.COD_PRODUCTO_1 = tbProd2.Text
        equi.CANTIDAD = CDec(tbCantidad1.Text)
        equi.EQUIVALE = CDec(tbCantidad2.Text)
        Return equi
    End Function

    Private Sub actuAgrega()
        Dim equi As GBDZafiro.Equivalencia = crearEqui()
        If GBDZafiro.Equivalencia.exisEqui(equi) Then
            GBDZafiro.Equivalencia.Actualizar(equi)

        Else
            GBDZafiro.Equivalencia.Nuevo(equi)
        End If
    End Sub

    Private Function eliminarEqui() As Boolean
        If msnBox.msnQuestion("¿Esta seguro de borrar la Equivalencia?") = Windows.Forms.DialogResult.Yes Then
            Dim equi As GBDZafiro.Equivalencia = crearEqui()
            GBDZafiro.Equivalencia.Eliminar(equi)
            Return True
            Exit Function
        End If
        Return False
    End Function

    Private Function buscarProducto() As GBDZafiro.Producto
        Dim oFrmBP As New FrmBProducto
        oFrmBP.BuscarPor = "CODIGO"
        Dim dlg As DialogResult = oFrmBP.ShowDialog()
        If dlg = DialogResult.OK Then
            Dim oProd As New GBDZafiro.Producto
            oProd = oFrmBP.oProducto
            Return oProd
            oFrmBP.Close()
            Exit Function
        End If
        Return Nothing
    End Function

    Private Sub cargargGvEqui()
        RemoveHandler gvEqui.CurrentCellChanged, AddressOf gvEqui_CurrentCellChanged

        gvEqui.DataSource = Nothing
        gvEqui.Rows.Clear()
        gvEqui.DataSource = GBDZafiro.Equivalencia.ConsultarAll()

        With gvEqui
            .Columns.Remove("ID_EQUIVALENCIA")

            .Columns("COD_PRODUCTO").HeaderText = "CODIGO"
            .Columns("ID_UNIDAD_MEDIDA").HeaderText = "UNIDAD"
            .Columns("COD_PRODUCTO_1").HeaderText = "CODIGO"
            .Columns("DESCRIPCION_1").HeaderText = "DESCRIPCION"
            .Columns("ID_UNIDAD_MEDIDA_1").HeaderText = "UNIDAD"

            .Columns("COD_PRODUCTO").DisplayIndex = 0
            .Columns("DESCRIPCION").DisplayIndex = 1
            .Columns("ID_UNIDAD_MEDIDA").DisplayIndex = 2
            .Columns("CANTIDAD").DisplayIndex = 3
            .Columns("COD_PRODUCTO_1").DisplayIndex = 4
            .Columns("DESCRIPCION_1").DisplayIndex = 5
            .Columns("ID_UNIDAD_MEDIDA_1").DisplayIndex = 6
            .Columns("EQUIVALE").DisplayIndex = 7

        End With

        AddHandler gvEqui.CurrentCellChanged, AddressOf gvEqui_CurrentCellChanged
    End Sub

    Private Sub cargarEqui()
        Dim rEqui As DataGridViewRow = gvEqui.CurrentRow
        tbProd1.Text = rEqui.Cells("COD_PRODUCTO").Value
        tbProd2.Text = rEqui.Cells("COD_PRODUCTO_1").Value
        tbDescripcion1.Text = rEqui.Cells("DESCRIPCION").Value
        tbDescripcion2.Text = rEqui.Cells("DESCRIPCION_1").Value
        tbUnd1.Text = rEqui.Cells("ID_UNIDAD_MEDIDA").Value
        tbUnd2.Text = rEqui.Cells("ID_UNIDAD_MEDIDA_1").Value
        tbCantidad1.Text = rEqui.Cells("CANTIDAD").Value
        tbCantidad2.Text = rEqui.Cells("EQUIVALE").Value
    End Sub

    Private Sub bloqCtrs()
        tbDescripcion1.Enabled = False
        tbDescripcion2.Enabled = False
        tbUnd1.Enabled = False
        tbUnd2.Enabled = False
        tbProd1.Enabled = False
        tbProd2.Enabled = False
    End Sub

End Class
